home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / Aztec C 5.0a disk 3.adf / crt_src / _main.c < prev    next >
C/C++ Source or Header  |  1989-10-27  |  4KB  |  189 lines

  1. /* Copyright (C) 1986,1987 by Manx Software Systems, Inc. */
  2.  
  3. /*
  4.  *    This is common startup code for both the CLI and the WorkBench.
  5.  *    When called from the WorkBench, argc is 0 and argv points to a
  6.  *    WBStartup type of structure.
  7.  */
  8.  
  9. #include <fcntl.h>
  10. #include <exec/alerts.h>
  11. #include <exec/memory.h>
  12. #include <libraries/dosextens.h>
  13. #include <workbench/startup.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <functions.h>
  17.  
  18. extern long _savsp, _stkbase;
  19.  
  20. extern int errno;
  21. extern int Enable_Abort;
  22.  
  23. extern int _argc, _arg_len;
  24. extern char **_argv, *_arg_lin;
  25. extern struct WBStartup *WBenchMsg;
  26.  
  27. extern struct _dev *_devtab;
  28. extern short _numdev;
  29.  
  30. void _cli_parse(struct Process *pp, long alen, char *aptr);
  31. void _wb_parse(struct Process *pp, struct WBStartup *WBenchMsg);
  32. int main(int argc, char **argv);
  33. #ifdef DETACH
  34. static void do_detach(long *alen, char **aptr);
  35. #endif
  36.  
  37. void
  38. _main(long alen, char *aptr)
  39. {
  40.     register struct Process *pp;
  41.  
  42. #ifdef DETACH
  43.     do_detach(&alen, &aptr);
  44. #endif
  45.  
  46.     if ((_devtab = AllocMem(_numdev*(long)sizeof(struct _dev),
  47.                                                     MEMF_CLEAR)) == 0) {
  48.         Alert(AG_NoMemory, 0L);
  49. #asm
  50.         move.l    __savsp,sp        ;get back original stack pointer
  51.         rts                        ;and exit
  52. #endasm
  53.     }
  54.  
  55.     _devtab[0].mode = O_RDONLY;
  56.     _devtab[1].mode = _devtab[2].mode = O_WRONLY;
  57.  
  58.     _stkbase = _savsp - *((long *)_savsp+1) + 8;
  59.     *(long *)_stkbase = 0x4d414e58L;
  60.  
  61.     pp = (struct Process *)FindTask(0L);
  62. #ifdef DETACH
  63.     if (alen) {
  64. #else
  65.     if (pp->pr_CLI) {
  66. #endif
  67.         _cli_parse(pp, alen, aptr);
  68.         Enable_Abort = 1;
  69. #ifndef DETACH
  70.         _devtab[0].mode |= O_STDIO;        /* shouldn't close if CLI */
  71.         _devtab[1].mode |= O_STDIO;
  72. #endif
  73.     }
  74.     else {
  75.         WaitPort(&pp->pr_MsgPort);
  76.         WBenchMsg = (struct WBStartup *)GetMsg(&pp->pr_MsgPort);
  77.         if (WBenchMsg->sm_ArgList) {
  78.             CurrentDir((struct FileLock *)WBenchMsg->sm_ArgList->wa_Lock);
  79.             _wb_parse(pp, WBenchMsg);
  80.         }
  81.         _argv = (char **)WBenchMsg;
  82.     }
  83.     _devtab[0].fd = Input();
  84.     if (_devtab[1].fd = Output())
  85.         _devtab[2].fd = Open("*", (long)MODE_OLDFILE);
  86.     exit(main(_argc, _argv));
  87. }
  88.  
  89. #ifdef DETACH
  90. extern long _stack, _priority, _BackGroundIO;
  91. extern char *_procname;
  92. BPTR _Backstdout = 0;
  93. extern struct FileLock *_detach_curdir;
  94. extern char *_detach_name;
  95. static long _alen = 0;
  96. static char *_aptr = 0;
  97.  
  98. static void
  99. do_detach(long *alen, char **aptr)
  100. {
  101.     register struct Process *pp;
  102.     register unsigned short c;
  103.     register char *cp;
  104.     register struct CommandLineInterface *cli;
  105.     register long l;
  106.     long *lp, *sav;
  107.     struct MemList *mm;
  108.     struct Library *savlib;
  109.  
  110.     pp = (struct Process *)FindTask(0L);
  111.     if (pp->pr_CLI) {            /* first time through!! */
  112.         CurrentDir(_detach_curdir = CurrentDir(0L));
  113.         _detach_curdir = DupLock(_detach_curdir);
  114.  
  115.         cli = (struct CommandLineInterface *) ((long)pp->pr_CLI << 2);
  116.         l = cli->cli_Module;
  117.         if ((savlib = OpenLibrary(DOSNAME, 33L)) == 0) {
  118.  
  119.             lp = (long *)*((long *)*((long *)*((long *)*((long *)
  120.                                             _savsp+2)+1)-3)-3)+107;
  121.             if (*lp != cli->cli_Module)
  122.                 exit(100);
  123.         }
  124.         else {
  125.             CloseLibrary(savlib);
  126.             lp = 0;
  127.         }
  128.         if (lp)
  129.             *lp = 0;
  130.         if (_stack == 0)
  131.             _stack = cli->cli_DefaultStack * 4;
  132.         if (_BackGroundIO)
  133.             _Backstdout = (BPTR)Open("*", (long)MODE_OLDFILE);
  134.         _alen = *alen;
  135.         _aptr = AllocMem(_alen, 0L);
  136.         memmove(_aptr, *aptr, (size_t)_alen);
  137.         cp = (char *)((long)cli->cli_CommandName << 2);
  138.         _detach_name = AllocMem((long)cp[0]+1, 0L);
  139.         memmove(_detach_name, cp, (size_t)cp[0]+1);
  140. #asm
  141.         move.l    __savsp,-(sp)
  142. #endasm
  143.         CreateProc(_procname, _priority, l, _stack);
  144.         cli->cli_Module = 0;
  145. #asm
  146.         move.l    (sp)+,sp
  147.         move.l    #0,d0
  148.         rts
  149. #endasm
  150.     }
  151.     else if (strcmp(pp->pr_Task.tc_Node.ln_Name, _procname) == 0) {
  152.                                                             /* second time */
  153.         lp = (long *)((long)pp->pr_SegList << 2);
  154.         lp = (long *)(lp[3] << 2);
  155.         sav = lp;
  156.         c = 2;
  157.         while (lp) {
  158.             lp = (long *)(*lp << 2);
  159.             c++;
  160.         }
  161.         mm = AllocMem((long)sizeof(struct MemList)+
  162.                             (c-1)*sizeof(struct MemEntry), 0L);
  163.         lp = sav;
  164.         mm->ml_NumEntries = c;
  165.         c = 0;
  166.         while (lp) {
  167.             mm->ml_me[c].me_Addr = (APTR)lp - 1;
  168.             mm->ml_me[c].me_Length = lp[-1];
  169.             lp = (long *)(*lp << 2);
  170.             c++;
  171.         }
  172.         mm->ml_me[c].me_Addr = (APTR)_aptr;
  173.         mm->ml_me[c++].me_Length = _alen;
  174.         mm->ml_me[c].me_Addr = (APTR)_detach_name;
  175.         mm->ml_me[c++].me_Length = _detach_name[0] + 1;
  176.  
  177.         AddTail(&pp->pr_Task.tc_MemEntry, &mm->ml_Node);
  178.  
  179.         CurrentDir(_detach_curdir);
  180.  
  181.         pp->pr_COS = _Backstdout;
  182.  
  183.         *alen = _alen;
  184.         *aptr = _aptr;
  185.     }
  186. }
  187. #endif
  188.  
  189.